package com.xmy.service.impl;

import com.xmy.dao.BaseDao;
import com.xmy.entity.Product;
import com.xmy.service.ProductService;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductServiceImpl extends BaseDao implements ProductService {
    @Override
    public List<Product> selAllProduct() {
        List<Product> products = new ArrayList<Product>();
        String sql = "select * from product";
        ResultSet rs = executeQuery(sql,null);
        try{
            while (rs.next()){
                Product product = new Product();
                product.setId(rs.getInt(1));
                product.setProductName(rs.getString(2));
                product.setQuantity(rs.getInt(3));
                products.add(product);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return products;
    }

    @Override
    public Product selProductCount(String name) {
        String sql = "select * from product where productName = ?";
        Object[] objects = {name};
        Product product = null;
        ResultSet rs = null;
        try{
            rs = executeQuery(sql,objects);
            while (rs.next()){
                product = new Product();
                product.setProductName(rs.getString(2));
                product.setQuantity(rs.getInt(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return product;
    }

    @Override
    public int updateProductCount(String name,int count) {
        String sql = "update product set quantity = ? where productName = ?";
        Object[] objects = {count,name};
        return executeUpdate(sql,objects);
    }
}
